<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\inspection\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 巡检报告管理
 * @package app\Record
 * @author LJQ<245369209@qq.com>
 */
class Record extends Api

{

    /**
   *  巡检报告列表
   * @param string  string operationId巡检员ID,如果有只显示巡检员自己的巡检任务列表.
   * @return Json
   */

	public function recordList($operationId = null){
            $where = [] ;
            if($operationId){
      
               $where['operationId'] = ['eq',$operationId];
             }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 巡检报告列表
			$info =collection(model('Record')->with('mission,item,user')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 巡检报告列表
			$info =collection(model('Record')->with('mission,item,user')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Record')->where($where)->count('id'); 
		  
    		    if (empty($info) && $count == 0) {
         			return json(['code'=>0,'msg'=>'没有找到巡检报告','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'巡检报告','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }

     
   /**
   *   查询巡检记录
   * @param keyword巡检报告名称
   * @param charge巡检员ID
   * @param status状态ID
   * @return Json
   */
   	public function search($keyword = null,$enterpriseId = null ,$start = null,$end = null){
        $where = [] ;
   		if($keyword){
   			$items = model('Item')->where('patrolPoint','like','%'.$keyword.'%')->select();
   			$itemIds = [];
   			foreach ($items as $value){
   				$itemIds[] = $value['id'];
   			}
   			$where['inspectionItemId'] = ['in',$itemIds];
   		}
   		if($enterpriseId){
   			$missions = model('Mission')->where('enterpriseId',$enterpriseId)->select();
   			$missionIds = [];
   			foreach ($missions as $value){
   				$missionIds[] = $value['id'];
   			}
   			$where['inspectionMissionId'] = ['in',$missionIds];
   		}
   		if($start&&$end){
   			$where['createTime'] = ['between time',[$start,$end]];
   		}
   		//分页参数
   		if(($limit = input('limit')) && ($page = input('page'))){
   			$startLimit =  ($page-1)*$limit;
   			// 巡检报告列表
   			$info =collection(model('Record')->with('mission,item,user')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
   		}else{
   			// 巡检报告列表
   			$info =collection(model('Record')->with('mission,item,user')->where($where)->select())->toArray();
   		}
	 	//总数
	    $count = model('Record')->with('mission,item,user')->where($where)->count('id'); 
		 
        if ($count == 0) {
              	return json(['code'=>1,'msg'=>'没有找到巡检报告','count'=>$count,'data'=>$info]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'巡检报告','count'=>$count,'data'=>$info]);
       	}
      }
      
      /**
       * 添加巡检任务报告
       * @param  string item 巡检项目id,
       * @return Json
       */
      public function add($data = NULL){
      	if($data == null){
      		$data = input('post.');
      	}
      	if (!$data['inspectionItemId']) {
      		return json(['code'=>0,'msg'=>'项目ID不存在.请检查']);
      	}
      
      
      	if (empty($data)) {
      		return json(['code'=>0,'msg'=>'数据异常,请检查']);
      	}
      	$model = model('Record');
      	$result = $model->change($data);
      	if($result !== false){
      		if(model('Item')->where('id',$data['inspectionItemId'])->update(['lastRecord'=>$model->id])){
                     setlog('Recordadd','Record',session('user_auth.id'));
      			return json(['code'=>1,'msg'=>'巡检报告添加成功']);
      		}
      	
      	}
      	return json(['code'=>0,'msg'=>'巡检报告添加失败,请检查']);
    }

}